#!/bin/bash
CURRENT_PATH=$(pwd);
ROOT_PATH=$(cd `dirname $0`/../; pwd);
cd $CURRENT_PATH;

# 核心测试：galaxy、utils，子模块测试：web、asteroid、satellite，性能测试：添加性能测试参数
#开始
function usage() {
	echo "usage: <exec> --param"
	echo "  type: core module shell"
	echo "  "
	echo "example:"
	echo "  core -bench               核心功能测试：性能测试"
	echo "  core [pattern]            核心功能测试：匹配测试"
	echo "  module asteroid           通信组件asteroid测试"
	echo "  module satellite -cover   通信组件satellite覆盖率测试"
}

if [[ $? != 0 ]] ; then usage >&2 ; exit 1 ; fi

CMD=$1;
shift;
bench=
cover=

e=0
case ${CMD} in
	core)
	    pp=$1
	    if [[ "${pp:0:1}" != "-" ]]
	    then
	        pattern=$1
	        shift;
	    fi
	;;
	module)
		module=$1;
		case ${module} in
            asteroid|satellite)
            ;;
            *)
                echo "module only support asteroid,satellite"
                exit
             ;;
        esac
		shift;
	;;
	*)
		usage
		exit
	;;
esac
name=${CMD}
if [[ "$module" != "" ]]
then
    name=${name}"_"${module}
fi

param=
np=0
for p in $*
do
    case ${p} in
        -bench)
            bench=1
        ;;
        -cover)
            cover=1;
            np=2
        ;;
        -*)
            param=${param}""${p}" "
            np=1
        ;;
        *)
            if [[ "${np}" = "1" ]]
            then
                param=${param}""${p}" "
            else
                if [[ "${np}" = "2" ]]
                then
                    case ${p} in
                        html)
                            cover_out="-o "${name}".html"
                        ;;
                        func)
                        ;;
                        *)
                            echo "cover only support html,func"
                            exit
                         ;;
                    esac
                    cover_type="-"${p}
                else
                    param=${param}""${p}" "
                fi
            fi
        ;;
    esac
done
if [[ "${module}" = "" ]]
then
    module="."
    ext_module="./utils ./utils/cache ./utils/orm ./utils/uuid ./server/**"
else
    module="./"${module}
fi
if [[ "${pattern}" != "" ]]
then
    pattern="-run "${pattern}
fi
if [[ "${bench}" = "1" ]]
then
    bench="-bench ${module}"
fi
if [[ "${cover}" = "1" ]]
then
    cover="-cover -coverprofile="${name}".profile.out"
fi
echo go test ${module} ${ext_module} ${bench} ${cover} ${pattern} ${param}
go test ${module} ${bench} ${cover} ${pattern} ${param}
e=$?

# if [ -z $e ]
#then
#    if [ "${cover}" != "" ]
#    then
#        go tool cover ${cover_type} ${cover_out}
#    fi
#fi
exit ${e}